/*
 * File Name:ms_tick.c
 * Version:v1.0.0
 * Change Logs:
 * Date             Author      	main change
 * 2025-03-12      	lanxin          the first version
 *
*/

/********************************************* READ ME BEGIN *************************************************************/

/* Brief:
 *
 * *1.模拟的 ms 时间戳。
 *
 */

/* Notice:
 *
 * *1.call ms_tick_increase() every milliseoncd。
 *
 */

/********************************************* READ ME END ***************************************************************/

/* source file */
#include "ms_tick.h"

/* debug define */

/* private define */

/* function declaration */

/* variable declaration */
static uint64_t tick_ms = 0;

MT_PUT_TO_RAM void ms_tick_increase ( void )
{
	tick_ms++;
}

MT_PUT_TO_RAM uint64_t ms_tick_get ( void )
{
	return tick_ms;
}

MT_PUT_TO_RAM void ms_tick_set ( uint64_t time )
{
	tick_ms = time;
}

MT_PUT_TO_RAM struct timeval ms_tick_get_timeval ( struct timeval *tv )
{
	struct timeval t_val;
	t_val.tv_sec = ( time_t ) ( tick_ms / 1000 );
	t_val.tv_usec = ( ( long long ) ( tick_ms ) % 1000 * 1000 );

	if ( tv )
	{
		tv->tv_sec = t_val.tv_sec;
		tv->tv_usec = t_val.tv_usec;
	}

	return t_val;
}

MT_PUT_TO_RAM void ms_tick_set_timeval ( const struct timeval *tv )
{
	if ( tv )
		tick_ms = 1000 * ( double ) ( tv->tv_sec ) + ( double ) tv->tv_usec / 1000;
}

MT_PUT_TO_RAM void ms_tick_set_time ( time_t *time )
{
	if ( time )
		tick_ms = 1000 * ( double ) ( *time );
}

MT_PUT_TO_RAM time_t ms_tick_get_time ( time_t *time )
{
	time_t t_now = ( time_t ) ( tick_ms / 1000 );

	if ( time )
		*time = t_now;

	return t_now;
}

MT_PUT_TO_RAM time_t ms_tick_check_second_time ( void )
{
	return ( tick_ms % 1000 == 0 ) ? ( time_t ) ( tick_ms / 1000 ) : 0;
}

/* 导出到初始化列表 */

/* 导出到 MSH 命令列表 */
